﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Shapes;

namespace 物品查看器.Converter
{
    public class RecipeItemHoldingHeightConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (double)value * 2;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class RecipeItemHoldingWidthConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(values[0] is double))
                return 0;
            double width = (double)values[0];
            int childCount = (int)values[1];
            return (childCount==0) ? width : width * childCount;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class RecipeItemXConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(values[0] is double))
                return 0;

            double width = (double)values[0];
            int col = (int)values[1];
            return width * col;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class RecipeItemYConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(values[0] is double))
                return 0;

            double height = (double)values[0];
            int row = (int)values[1];
            return height * 2 * row;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class NetLinesConverter : IMultiValueConverter
    {

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            NetItemView view = values[0] as NetItemView;
            double size = (double)values[1];
            double thickness = 2;

            if (view is null)
                return null;

            List<Line> lines = new List<Line>();
            foreach (NetItemView child in view.Children)
            {
                int i = view.Children.IndexOf(child);
                Line line = new Line();
                int offset = 0;
                if (i > 0)
                {
                    Line l = new Line();
                    l.X1 = (view.Children[i - 1].Col - view.Col + 0.5) * size;
                    l.Y1 = size * 1.5;
                    l.X2 = (child.Col - view.Col + 0.5) * size;
                    l.Y2 = size * 1.5;
                    l.Stroke = Brushes.Black;
                    l.StrokeThickness = thickness;
                    lines.Add(l);

                    offset = child.Col - view.Col;
                }
                else
                {
                    Line l = new Line();
                    l.X1 = size * 0.5;
                    l.Y1 = size;
                    l.X2 = size * 0.5;
                    l.Y2 = size * 1.5;
                    l.Stroke = Brushes.Black;
                    l.StrokeThickness = thickness;
                    lines.Add(l);
                }
                line.X1 = size * (offset + 0.5);
                line.Y1 = size * 1.5;
                line.X2 = line.X1;
                line.Y2 = size * 2;
                line.StrokeThickness = thickness;
                line.Stroke = Brushes.Black;

                lines.Add(line);
            }

            return lines;
        }
      
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class YieldSpeedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is NetItemView))
                return 0;

            NetItemView view = value as NetItemView;
            return view.Yield / view.PlantRequire;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
